package cn.liyunqi.ttv.main.controller;

import cn.liyunqi.ttv.main.constant.Constants;
import cn.liyunqi.ttv.main.constant.VersionEnum;
import cn.liyunqi.ttv.main.localdb.ConfigHelper;
import cn.liyunqi.ttv.main.vo.UserPreference;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;

import java.net.URL;
import java.util.ResourceBundle;

public class SetUpUIController extends BaseFXController {

    @FXML
    private Label versionLab;

    @FXML
    private CheckBox autoDelHistory;

    @FXML
    private CheckBox autoSaveHistory;

    private MainUIController mainUIController;

    public void setMainUIController(MainUIController mainUIController) {
        this.mainUIController = mainUIController;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {


        versionLab.setText("v" + VersionEnum.LOCAL_VERSION.getVersion());


        UserPreference value = ConfigHelper.getUserPreferenceByUserId(Constants.userId);

        if(value != null){
            autoDelHistory.setSelected(value.getAutoDelHistory() == null?true:0 != value.getAutoDelHistory());
            autoSaveHistory.setSelected(value.getAutoSaveHistory() == null?true:0 != value.getAutoSaveHistory());
        }else{
            autoDelHistory.setSelected(true);
            autoSaveHistory.setSelected(true);
        }

        autoSaveHistory.setOnAction(new EventHandler() {
            @Override
            public void handle(Event event) {
                ConfigHelper.insertOrUpdateUserPreferenceSingleByUserId(Constants.userId,"auto_save",autoSaveHistory.isSelected()?"1":"0");
            }
        });
        autoDelHistory.setOnAction(new EventHandler() {
            @Override
            public void handle(Event event) {
                ConfigHelper.insertOrUpdateUserPreferenceSingleByUserId(Constants.userId,"auto_del",autoDelHistory.isSelected()?"1":"0");
            }
        });


    }
}
